package com.pilgrim.framework.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 * DateCommonUtils
 *
 * @author wangyibin
 * date 2020/3/6 17:04
 */
public class DateUtil {

    public static final String FORMATE_DEFULT = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_YYYYMMDD = "yyyy-MM-dd";
    public static final String FORMAT_YYYYMM = "yyyy-MM";
    public static final String FORMAT_MMDD = "MM-dd";

    /**
     * Date类型转换为String类型
     *
     * @param date       Date类型的时间
     * @param formatType 格式
     * @return 时间字符串
     */
    public static String dateToString(Date date, String formatType) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatType);
        return sdf.format(date);
    }

    /**
     * Date类型转换为默认格式的String类型
     *
     * @param date 时间
     * @return 时间字符串
     */
    public static String dateToDefaultString(Date date) {
        return dateToString(date, FORMATE_DEFULT);
    }

    /**
     * String类型时间转化为Date类型
     *
     * @param dateString 时间字符串
     * @param formatType 时间格式化类型
     * @return 时间
     */
    public static Date stringToDate(String dateString, String formatType) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatType);
        Date date = null;
        try {
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            throw new RuntimeException("时间格式转化错误！");
        }
        return date;
    }

    /**
     * String类型时间转化为默认格式的Date类型
     *
     * @param dateString 默认格式时间字符串
     * @return 时间
     */
    public static Date stringToDefaultDate(String dateString) {
        return stringToDate(dateString, FORMATE_DEFULT);
    }

    /**
     * 计算两个时间的间隔秒数
     *
     * @param startTime 起始时间
     * @param endTime   结束时间
     * @return 秒
     */
    public static int getDistanceTime(Date startTime, Date endTime) {
        Date now = new Date();
        startTime = startTime == null ? now : startTime;
        endTime = endTime == null ? now : endTime;
        return (int)((endTime.getTime() - startTime.getTime()) / 100);
    }

    /**
     * 日期增加
     *
     * @param date   日期
     * @param field  增加类型 Calendar
     * @param amount 增加数量
     * @return day
     */
    public static Date add(Date date, int field, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field, amount);
        return calendar.getTime();
    }

    /**
     * 获取某日开始时间
     *
     * @param date 传入日期
     * @return 返回该日开始时间 (00:00:00)
     */
    public static Date startDayTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取某日结束时间
     *
     * @param date 传入日期
     * @return 返回该日结束时间 (23:59:59)
     */
    public static Date endDayTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * LocalDateTime转Date
     *
     * @param localDateTime localDateTime
     * @return Date
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

    }

    /**
     * LocalDate转为Date.
     *
     * @param date date
     * @return LocalDateTime
     */
    public static LocalDateTime date2LocalDate(Date date) {
        if (null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
}
